import { Injectable } from '@nestjs/common';
import { PrismaService } from 'src/prisma/prisma.service';
import { TowerService } from 'src/tower/tower.service';
import { CreateBetDto } from './dto/create-bet.dto';
import { UpdateBetDto } from './dto/update-bet.dto';

@Injectable()
export class BetService {
  constructor(
    private prisma: PrismaService,
    private towerService: TowerService,
  ) {}

  async createMany(createBetDtos: CreateBetDto[]) {
    const lineId = createBetDtos[0].lineId;
    const towers = await this.towerService.findAll({ lineId });
    const data = createBetDtos.map((bet) => {
      const { btName } = bet;
      const [tn1, tn2] = btName.split('--');
      const leftTowerId = towers.find((tower) => tower.tName === tn1)?.id!;
      const rightTowerId = towers.find((tower) => tower.tName === tn2)?.id!;

      return {
        ...bet,
        leftTowerId,
        rightTowerId,
      };
    });
    await this.prisma.bet.createMany({ data });
  }

  async create(createBetDto: CreateBetDto) {
    return 'This action adds a new bet';
  }

  async findAll(args: Record<string, any>) {
    const { lineId } = args;
    return await this.prisma.bet.findMany({
      where: { lineId: +lineId },
      include: {
        CalcBet: true,
      },
    });
  }

  findOne(id: number) {
    return `This action returns a #${id} bet`;
  }

  update(id: number, updateBetDto: UpdateBetDto) {
    return `This action updates a #${id} bet`;
  }

  remove(id: number) {
    return `This action removes a #${id} bet`;
  }
}
